// pages/coulumnList/coulumnList.js

const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    arr:[],
    anthologyArr:[],
    allArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this ;
    app.bind();
    console.log(options)
    // let str = JSON.parse(options.str);
    page.setData({
      lesson_id: options.lesson_id
    })
    page.getDetail();

    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this ;
    // page.getChoice();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPageStr();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    console.log(that.data.count);
    console.log(that.data.page);
    
      // that.setData({
      //   page: that.data.count - 1
      // });
      console.log(that.data.page);
    
    // that.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });
    console.log(55555555555)
    // if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
      console.log(that.data.page)
    // }

    that.getDetail();
    // 隐藏加载框  
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var url = app.getcurrentPageStr();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "&user_id=" + user_id,
      title: '得道1314'
    };
  },

  //获取列表
  getDetail: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.index.lessonInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        lesson_id: that.data.lesson_id,
        page:that.data.page
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            list: res.data.data.list,
            info: res.data.data.info,
            row_count: res.data.data.row_count
          })
          
          
         
          console.log(that.data.row_count);
          Math.ceil(that.data.row_count / 10);
          that.setData({
            count: Math.ceil(that.data.row_count / 10)
          })
          console.log(that.data.count)
          
          
          if(that.data.page == 1){
            that.getChoice()
          }
          
          

          for (var i = 0; i < that.data.list.length; i++) {

            that.data.list[i].num = i
          }

          that.setData({
            list: that.data.list
          })
          
          for (var i = 0; i < that.data.list.length; i++) {

            if (that.data.row_count == that.data.arr.length) {

            } else {
              that.data.arr.push(that.data.list[i]);
              that.setData({
                arr: that.data.arr
              })
            }

          }


        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }
      }
    });

  },

  getInfoDetail:function(){
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.index.lessonInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        lesson_id: that.data.lesson_id,
        page: that.data.allpage
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {

          that.setData({
            allList:res.data.data.list
          })
         
          for (var i = 0; i < that.data.allList.length; i++) {
            var a = {};

          a.title = that.data.allList[i].stage
          a.id = that.data.allList[i].id

          that.data.anthologyArr.push(a);
          that.setData({
            anthologyArr: that.data.anthologyArr
          })



          }

          that.setData({
            changeId: that.data.anthologyArr[0].id
          })


        } else if (res.data.code == -1) {

        }
      }
    });
  },

  // 获取选节
  getChoice:function(){
    var page = this;
    for(var i = 0 ; i < page.data.count ; i ++){
      console.log(i)
      var allpage = i + 1;
      console.log(allpage)
      page.setData({
        allpage:allpage
      })
      page.getInfoDetail()
    }
    
  },

  //跳转到详情
  toCourse: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var proList = this.data.arr;
    // let str = JSON.stringify(proList[index]);
    var section_id = proList[index].id;
    var lesson_id = proList[index].lesson_id;
    console.log(section_id);
    console.log(lesson_id)
    wx.navigateTo({
      url: '/pages/freeDetail/freeDetail?section_id=' + section_id + '&lesson_id=' + lesson_id
    })
  },

  //跳转到首页
  toIndex:function(){
    wx.switchTab({
      url:'/pages/index/index'
    })
  },

  // 跳转到详情
  toDetail:function(res){
    var page = this
    console.log(res)
    var index = res.currentTarget.dataset.index
    var lesson_id = page.data.info.id
    var section_id = page.data.anthologyArr[index].id
    wx.navigateTo({
      url: '/pages/freeDetail/freeDetail?section_id=' + section_id + '&lesson_id=' + lesson_id
    })
  }


})